package com.txm.qqserver.service;

import java.util.Collection;
import java.util.HashMap;

public class ManageClientThreads {
    private static HashMap<String, Thread> mcts = new HashMap<>();

    public static HashMap<String, Thread> getMcts() {
        return mcts;
    }

    public Thread getMctsByKey(String key) {
        return mcts.get(key);
    }

    public static void setMcts(String key, Thread thread) {
        mcts.put(key, thread);
//        HashMap<String, Thread> handleHashMap = new HashMap<>(mcts);
//        handleHashMap.remove(key);
//        Collection<Thread> values = handleHashMap.values();
//        for (Thread val : values) {
//            UserServerService.handleUserThread((ServerConnectServerThread) val);
//        }
    }

    public static Thread removeMctsByKey(String key) {
        Thread thread = mcts.get(key);
        mcts.remove(key);
        return thread;
    }

    public ServerConnectServerThread isInThreads(String username) {
        // 判断线程是否已存在
        ServerConnectServerThread thread = null;
        Collection<Thread> values = mcts.values();
        for (Thread val : values) {
            if (((ServerConnectServerThread) val).user.getUsername().equals(username)) {
                thread = (ServerConnectServerThread) val;
                break;
            }
        }
        return thread;
    }
}
